<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="com.xiaoyu.dao.BaseDao" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="com.xiaoyu.pojo.Person" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>通讯录</title>
    <link rel="stylesheet" href="static/css/page.css">
    <script type="text/javascript" src="static/js/page.js" charset="UTF-8"></script>
</head>
<body>
    <%
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        Object[] params ={};
        try {
            conn = BaseDao.getConnection();
            String sql = "Select * from `contact`";
            pst = conn.prepareStatement(sql);
            rs = BaseDao.execute(conn, sql, params, rs, pst);
            ArrayList<Person> list = new ArrayList<Person>();
            while(rs.next()){
                Person person = new Person();
                person.setID(rs.getInt("ID"));
                person.setName(rs.getString("name"));
                person.setSex(rs.getString("sex"));
                person.setAge(rs.getInt("age"));
                person.setPhone(rs.getString("phone"));
                list.add(person);
            }
            request.setAttribute("list", list);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.release(conn, pst, rs);
        }
    %>
    <div class="container">
        <h1>通讯录</h1>
        <div class="clearfix">
            <a class="search" href="search.jsp"><button>搜索</button></a>
            <a class="search" href="AjaxSearch.jsp"><button>Ajax搜索</button></a>
        </div>
        <div class="table">
            <table id="table">
                <tr>
                    <th>name</th>
                    <th>sex</th>
                    <th>age</th>
                    <th>phone</th>
                    <th>&nbsp;</th>
                    <th>&nbsp;</th>
                </tr>
                <%
                    ArrayList<Person> list=(ArrayList<Person>)request.getAttribute("list");
                    if (list == null || list.size() < 1) {
                        out.print("没有数据！");
                    } else {
                        for (Person person : list) {
                %>
                <tr>
                    <td><%=person.getName()%></td>
                    <td><%=person.getSex()%></td>
                    <td><%=person.getAge()%></td>
                    <td><%=person.getPhone()%></td>
                    <td>
                        <a href="${pageContext.request.contextPath}/deletePerson?id=<%=person.getID()%>">删除</a>
                    </td>
                    <td>
                        <a href="${pageContext.request.contextPath}/updatejump?id=<%=person.getID()%>">修改</a>
                    </td>
                </tr>
                <%
                        }
                    }
                %>
            </table>
        </div>

        <div class="add">
            <p>添加新成员</p>
            <form action="${pageContext.request.contextPath}/addPerson" method="post" onsubmit="return checkTel()">
                <div class="clearfix"><span>name:</span> <input class="item" type="text" name="name"></div>
                <div class="clearfix"><span>sex:</span> <input class="item" type="text" name="sex"></div>
                <div class="clearfix"><span>age:</span> <input class="item" type="text" name="age"></div>
                <div class="clearfix"><span>phone:</span> <input class="item" type="text" name="phone" id="tel"></div>
                <input type="submit">
            </form>
        </div>
    </div>
</body>
</html>
